package com.situ.orderingsystem.dao.impl;

import com.situ.orderingsystem.dao.EmployeeDao;
import com.situ.orderingsystem.global.Global;
import com.situ.orderingsystem.model.Employee;
import com.situ.orderingsystem.model.EmployeeSerchBean;
import com.situ.orderingsystem.utils.PaginateInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class EmployeeDaoImpl implements EmployeeDao {
    private  final JdbcTemplate template = new JdbcTemplate(Global.getDataSource());
    @Override
    public List<Employee> findAll(EmployeeSerchBean es, PaginateInfo pi) {
        String countsql ="select count(0) from employee";
        String sql = "select id,emp_id,name,phone,description from employee";
        List<Object> objectList = new ArrayList<>();

        StringBuilder whysql = new StringBuilder();
        if(es != null){
            whysql.append(" where 1 = 1 and is_deleted = 0");
            if(es.getEmpId() != null){
                whysql.append(" and emp_id = ?");
                objectList.add(es.getEmpId());
            }
            if(StringUtils.hasText(es.getName())){
                whysql.append(" and name like ?");
                objectList.add("%"+es.getName()+"%");
            }
            if(StringUtils.hasText(es.getPhone())){
                whysql.append(" and phone = ?");
                objectList.add(es.getPhone());
            }
            if(StringUtils.hasText(es.getDescription())){
                whysql.append(" and description like ?");
                objectList.add("%"+es.getDescription()+"%");
            }
        }
        countsql += whysql.toString();
        Long count = template.queryForObject(countsql,Long.class,objectList.toArray());
        pi.setCount(count);
        whysql.append(" limit ?, ?");
        objectList.add(pi.getOffset());
        objectList.add(pi.getLimit());
        sql+=whysql;
        List<Employee> list = template.query(sql,new BeanPropertyRowMapper<>(Employee.class),objectList.toArray());
        return list;
    }
    public List<Employee> findAll2() {
        String sql = "select id,emp_id,name,phone,description from employee";
        List<Employee> list = template.query(sql,new BeanPropertyRowMapper<>(Employee.class));
        return list;
    }
    @Override
    public boolean deleteById(Integer id) {
        String sql ="update employee set is_deleted = 1 where id = ?";
        int rows = template.update(sql,id);
        return  rows>0;

    }


    @Override
    public int  add(Employee employee) {
        String sql = "insert into employee" +
                "(emp_id,`name`,phone,description)" +
                "values (?,?,?,?)";
        int rows = template.update(sql,employee.getEmpId(),employee.getName(),employee.getPhone(),employee.getDescription());
        return rows;
    }

    @Override
    public Employee searchById(Integer id) {
        String sql = "select emp_id,`name`,phone,description from employee where id = ?";
        List<Employee> emp = template.query(sql,new BeanPropertyRowMapper<>(Employee.class),id);
        return emp.get(0);
    }

    @Override
    public int update(Employee employee) {
        String sql ="update employee set emp_id = ? , name = ? ,phone = ? ,description = ? where id = ?";
        int rows = template.update(sql,employee.getEmpId(),employee.getName(),employee.getPhone(),employee.getDescription(),employee.getId());
        return  rows ;
    }
}
